﻿using StockTradTest.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace StockTradTest.strategy
{
    public partial class MyStrategy
    {
        //多股票交易
        //优化过的标准Aberration测试
        public static void TradeMultiAberrationTest(int n, bool isRecord = false, int startDate = 0, int endDate = int.MaxValue)
        {
            var stockInfo = Base.StockInfoDatabase.GetAllStockInfoAsync().Result;
            if (stockInfo == null) return;
            List<string> codes = new List<string>();
            List<List<Stock>> stocksList = new List<List<Stock>>();
            List<List<Indicator>> indicatorsList = new List<List<Indicator>>();
            foreach (var info in stockInfo)
            {
                if (!info.Name.Contains("银行")) continue;
                Funciton.UpdateStockDay(info.Code).Wait();

                Base.CurrentCode = info.Code;
                codes.Add(info.Code);
                List<Stock> stocks = Base.StockDatabase.GetStockAsync(startDate, endDate).Result;
                stocksList.Add(stocks);
                List <Indicator> indicators = null;
                CalcIndicator.CalcBoll(n, stocks, ref indicators);
                indicatorsList.Add(indicators);

            }
            string record = "";
            bool inBuyStatus = true;
            for (int j = 0; j < stocksList.Count;j++)
            {
                stocks = stocksList[j];
                List<Indicator> indicators = indicatorsList[j];
                if (stocks[0].Date != indicators[0].Date)
                {
                    Console.WriteLine("{0} != {1}.", stocks[0].Date, indicators[0].Date);
                    return;
                }
                TestTradBase testTradBase = new TestTradBase();
                testTradBase.InitalMyAccount();
                //Error
                for (int i = 0; i < stocks.Count; i++)
                {
                    testTradBase.date = stocks[i].Date;
                    if (i < n)
                    {
                        continue;
                    }
                    //如果在买入过程中
                    if (inBuyStatus)
                    {
                        //突破高线就买，实际运行时候为当前价
                        if (stocks[i].HighPrice > indicators[i - 1].BollUp)
                        {
                            inBuyStatus = !testTradBase.BuyAtMarket(codes[j]);
                            
                        }
                    }
                    else
                    {
                        //跌破中线就卖
                        if (stocks[i].LowPrice < indicators[i - 1].BollMd)
                        {
                            inBuyStatus = testTradBase.SellAtMarket(codes[j]);

                        }
                    }

                }
                inBuyStatus = true;
                string temp = string.Format("{0}  {1} 总资产 {2}\n", codes[j], stocks[stocks.Count-1].Date, testTradBase.GetAllMyAsset());
                if (isRecord) record += temp;
                Console.WriteLine(temp);
            }
            TextRecord.Record(record);
            TextRecord.OpenRecord();
        }
    }
 
}
